#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
	int i = 0;
	for (i = 0;i < row;i++)
	{
		int j = 0;
		for (j = 0;j < col;j++)
		{
			 board[i][j] = set;//引入set，一个函数操控两个数组
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf("---------扫雷游戏----------\n");
	for (i = 0;i <= col;i++)
	{
		printf("%d ", i);//操控列数，打印最上层横坐标
	}
	printf("\n");
	for (i = 1;i <= row;i++)
	{
		printf("%d ", i);//操控行数，打印最左边纵坐标
		int j = 0;
		for (j = 1;j <= col;j++)//j从1开始，向右移动一格
		{
			printf("%c ",board[i][j]);
		}
		printf("\n");
	}
	printf("---------扫雷游戏----------\n");
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
	//布置10个雷
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')//防止重复
		{
			board[x][y] = '1';
			count--;//控制循环
		}
	}
}

GetMineCount(char mine[ROWS][COLS], int x, int y)
{ 
	return (mine[x - 1][y - 1] +
		mine[x - 1][y] +
		mine[x - 1][y + 1] +
		mine[x][y - 1] +
		mine[x][y + 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] - 8 * '0');//'1'-'0'= 1
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	printf("输入样例：x(空格)y   （x代表行，y代表列）\n");
	printf("输入完成后请敲回车键\n"); 
	while (win < row * col - EASY_COUNT)
	{
		next:
		printf("请输入需要排查的坐标:>\n");
		scanf("%d %d", &x, &y);
		
			if (x >= 1 && x <= row && y >= 1 && y <= col)//保证不越界
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾，你被炸死了\n");
					DisplayBoard(mine, ROW, COL);
					printf("上表中1代表雷，0代表安全\n");
					break;

				}
				else
				{
					if ((show[x][y] == '0')||(show[x][y] == '1')||(show[x][y] == '2') || (show[x][y] == '3') || (show[x][y] == '4') || (show[x][y] == '5') || (show[x][y] == '6') || (show[x][y] == '7') || (show[x][y] == '8'))
						{
							printf("该坐标已输入，请重新输入\n");
							goto next;
						}
					int count = GetMineCount(mine, x, y);
					show[x][y] = count + '0';//'0'+1 ='1'
					DisplayBoard(show, ROW, COL);
					win++;
					
				}
			}
			else
			{
				printf("非法坐标，请重新输入\n");
			}
		
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你，排雷成功\n");
		DisplayBoard(mine, ROW, COL);
		printf("上表中1代表雷，0代表安全\n");
	}
}